package com.atguigu.lease.web.admin.controller.user;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.UserInfoService;
import com.atguigu.lease.web.admin.vo.user.UserInfoQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户信息管理！(UserInfoController)")
@RestController
@RequestMapping("/admin/user")
public class UserInfoController {

    @Autowired
    private UserInfoService service;

    //单表操作不用写sql
    @Operation(summary = "分页查询用户信息")
    @GetMapping("page")
    public Result<IPage<UserInfo>> pageUserInfo(@RequestParam long current, @RequestParam long size, UserInfoQueryVo queryVo) {
//        Page<UserInfo> page = service.page(new Page<UserInfo>(current, size), new LambdaQueryWrapper<UserInfo>().like(queryVo.getPhone() != null, UserInfo::getPhone, queryVo.getPhone()).eq(queryVo.getStatus() != null, UserInfo::getStatus, queryVo.getStatus()));
        LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userInfoLambdaQueryWrapper.like(queryVo.getPhone() != null, UserInfo::getPhone, queryVo.getPhone());
        userInfoLambdaQueryWrapper.eq(queryVo.getStatus() != null, UserInfo::getStatus, queryVo.getStatus());
        Page<UserInfo> page = service.page(new Page<>(current, size), userInfoLambdaQueryWrapper);
        return Result.ok(page);
    }

    @Operation(summary = "根据用户id更新账号状态")
    @PostMapping("updateStatusById")
    //这个用update因为没涉及到保存 保存或修改的话所有情况都是需要先查到，所以saveOrUpdate需要传入一个UserInfo
    public Result updateStatusById(@RequestParam Long id, @RequestParam BaseStatus status) {
        service.update(new LambdaUpdateWrapper<UserInfo>().set(UserInfo::getStatus, status).eq(UserInfo::getId, id));
        return Result.ok();
    }
}
